#!/usr/bin/env lua
require(arg[1])
require 'moonunit'
require 'apr.std.table'
rtest_stats = {tests = 0, success = 0, failure = 0}
env = getfenv()

keys = apr.std.table.keys(env)
for _, k in ipairs(keys) do
    local v = env[k]
    if getmetatable(v) == moonunit.TestCase then
        print('\n -- ' .. k .. " -- ")
        local success, failure = v:run({})
        rtest_stats.tests = rtest_stats.tests + success + failure
        rtest_stats.success = rtest_stats.success + success
        rtest_stats.failure = rtest_stats.failure + failure
    end
end

if rtest_stats.failure > 0 then
    print('\27[31m' .. rtest_stats.failure .. '/' .. rtest_stats.tests .. ' tests failed\27[39m')
else
    print('\27[32m' .. rtest_stats.failure .. '/' .. rtest_stats.tests .. ' tests failed\27[39m')
end
